<template>
  <div>
    <el-row type="flex" :gutter="10" class="row-bg">
      <el-col :span="5">
        <div style="margin-top: 10px;margin-left: -10px;">
          <!--<i style="color: #00a65a" class="el-icon-plus" @click="() => addRoot()"></i>-->
          <el-tree
            :default-expand-all="true"
            @check="checkChange"
            show-checkbox
            ref="tree"
            :props="props1"
            :data="data"
            node-key="id"
            :expand-on-click-node="false"
            @node-click="select">
      <span class="custom-tree-node" slot-scope="{ node, data }" v-on:mouseenter="showEdit(node)"
            v-on:mouseleave="hideEdit(node)">
          <span style="display: inline-block;padding-right: 0px;">&nbsp;<span v-if="node.data.select" style="color: red">{{ node.label}}</span><span
            v-if="!node.data.select">{{ node.label}}</span></span>
        <!--<icon-svg name="fileopen"></icon-svg>-->
        <span v-if="node.data.s_edit || (node.data.name ==='全部' && node.data.parentId === null )">
        <i style="color: #00a65a" class="el-icon-plus" @click="() => addNode(node)"></i>
        <i style="color: #286aa6" class="el-icon-edit" @click="() => edit(node)"></i>
        <i v-if=" node.data.children.length === 0" style="color: #a6300a" class="el-icon-delete"
           @click="() => delNode(node, data)"></i>
        </span>
      </span>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="19">
  <div class="mod-config">
    <el-form size="mini" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-col :span="4">
      <el-form-item >
        <el-input v-model="dataForm.key" placeholder="名称"  clearable @change="getDataList()"></el-input>
      </el-form-item>
      </el-col>
      <el-col :span="4">
      <el-form-item>
        <el-input v-model="dataForm.mac" placeholder="MAC" clearable @change="getDataList()"></el-input>
      </el-form-item>
      </el-col>
      <el-col :span="4">
      <el-form-item>
        <el-select v-model="dataForm.switchStatus" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      </el-col>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('generator:stb:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('generator:stb:save')" type="warning" @click="lead = true">Excel导入</el-button>
        <el-button v-if="isAuth('generator:stb:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
      <!--<el-form-item>-->
          <!--<span style="color: #008000">{{count.onlineCount}}</span> / <span style="color: #141d80">{{count.all}}</span>-->
      <!--</el-form-item>-->
    </el-form>
    <el-form size="mini" :inline="true">
      <el-form-item>
        <!--<el-button @click="addPlayHandle()">加播</el-button>-->
        <el-button @click="cleanPrograme">清空节目</el-button>
        <!--<el-button @click="subPlayHandle">减播</el-button>-->
        <el-button @click="remoteInstall(1)">远程安装</el-button>
        <!--<el-button @click="remoteInstall(0)">远程卸载</el-button>-->
        <el-button @click="setVolHandle">音量设置</el-button>
        <el-button @click="msgHandle">插播消息</el-button>
        <el-button @click="screenShotHandle">截屏</el-button>
        <el-button @click="switchSet()">定时开关机</el-button>
        <el-button @click="remoteBoot(2)">开屏</el-button>
        <el-button @click="remoteBoot(0)">关屏</el-button>
        <el-button @click="remoteBoot(1)">重启</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      size="small"
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="id"
      @select="selectChangeHandle"
      @select-all="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="30"
        reserve-selection
      >
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="mac"
        header-align="center"
        align="center"
        label="MAC">
      </el-table-column>
      <el-table-column
        prop="groupName"
        header-align="center"
        align="center"
        label="分组">
      </el-table-column>
      <el-table-column
        prop="switchStatus"
        header-align="center"
        align="center"
        label="状态"
        :width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.switchStatus === 0" type="info" size="small">禁用
          </el-tag>
          <el-tag v-if="scope.row.switchStatus === 1" type="warning" size="small">正常
          </el-tag>
          <i v-if="scope.row.switchStatus === 1" class="el-icon-error" @click="changeStatus(scope.row.id)"></i>
          <i v-if="scope.row.switchStatus === 0" class="el-icon-success" @click="changeStatus(scope.row.id)"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="onlineStatus"
        header-align="center"
        align="center"
        label="在线"
        :width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.onlineStatus === 0" type="info" size="small">离线</el-tag>
          <el-tag v-if="scope.row.onlineStatus === 1" type="warning" size="small">在线</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="width"
        header-align="center"
        align="center"
        label="分辨率（宽/高）">
        <template slot-scope="scope">
        <span v-show="scope.row.width">{{scope.row.width}} / {{scope.row.height}}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="mini" @click="view(scope.row.id)">详情</el-button>
          <!--<el-button type="text" size="mini" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button type="text" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <sub-play v-if="subPlayVisible" ref="subPlay"></sub-play>
    <add-play v-if="addPlayVisible" ref="addPlay"></add-play>
    <set-vol ref="setVol" ></set-vol>
    <screen-shot ref="screenShot"></screen-shot>
    <rollmsg-add ref="msg"></rollmsg-add>
    <!-- 弹窗, 声音设置 -->
    <stb-switch  ref="stbSwtich"></stb-switch>
    <!-- 弹窗, 远程安装 / 卸载 -->
    <stb-install v-if="stbInstallVisible" ref="stbInstall"></stb-install>
  </div>
      </el-col>
    </el-row>
    <el-dialog title="新增目录" :visible.sync="showAddGroup">
      <el-form :model="curNode">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="curNode.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddGroup = false">取 消</el-button>
        <el-button type="primary" @click="saveRoot">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分组重命名" :visible.sync="showEditGroup">
      <el-form :model="curNode">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="curNode.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showEditGroup = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑名称" :visible.sync="editName">
      <el-form :model="curNode">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="curNode.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddGroup = false">取 消</el-button>
        <el-button type="primary" @click="saveRoot">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="批量导入"
      :visible.sync="lead"
      width="50%"
    >
      <el-form ref="leadForm" label-width="80px">
        <el-form-item>
          <el-button type="primary" @click="leadHandle()">下载模版</el-button>
        </el-form-item>
        <el-form-item  label="导入Excel" prop="path">
          <el-upload ref="up_source"
                     class="upload-demo"
                     :on-success="sourceSuccess"
                     drag
                     :action="getUrl()"
                     multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将Excel文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">导入记录 (Excel文件需按照模版格式书写)</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<style scoped>
  .el-form-item{
    margin-bottom: 5px!important;
  }
</style>
<script>
  import AddOrUpdate from './stb-add-or-update'
  import SubPlay from './sub-play'
  import AddPlay from './add-play'
  import SetVol from './set-vol'
  import StbInstall from './stb-install'
  import ScreenShot from './screen-shot'
  import RollmsgAdd from './rollmsg-add-or-update'
  import StbSwitch from './stb-switch'
  export default {
    data: function () {
      return {
        dataForm: {
          key: '',
          mac: '',
          switchStatus: ''
        },
        options: [
          {
            value: '',
            label: '全部'
          }, {
            value: '0',
            label: '禁用'
          }, {
            value: '1',
            label: '启用'
          }],
        props1: {
          label: 'name',
          children: 'children',
          isLeaf: 'leaf'
        },
        count: {},
        data: [],
        showAddGroup: false,
        showEditGroup: false,
        editName: false,
        curNode: {},
        selectedNode: {},
        pNode: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        subPlayVisible: false,
        addPlayVisible: false,
        setVolVisible: false,
        stbInstallVisible: false,
        lead: false,
        leadForm: {
        }
        // sourceUPurl: this.getUrl()
      }
    },
    components: {
      StbSwitch,
      RollmsgAdd,
      ScreenShot,
      SetVol,
      AddPlay,
      SubPlay,
      AddOrUpdate,
      StbInstall
    },
    activated () {
      this.getDataList()
    },
    mounted () {
      this.getGroups()
      this.getCount()
    },
    methods: {
      getUrl () {
        var gid = this.selectedNode.data != null ? this.selectedNode.data.id : 0
        return this.$http.adornUrl('/generator/stb/lead?gid=' + gid + '&token=' + this.$cookie.get('token'))
      },
      getCount () {
        const url = '/generator/stb/count'
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.count = data.data
          } else {
            // alert(data.msg)
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      // 获取数据列表
      getDataList: function () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/stb/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.key,
            'mac': this.dataForm.mac,
            'switchStatus': this.dataForm.switchStatus,
            'stb_group_id': this.selectedNode.data ? this.selectedNode.data.id : 0
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        console.log('val', val)
        this.dataListSelections = val
        this.selectStbChangeAll(this.data)
      },
      selectChangeHandle (vals, val) {
        console.log(val)
        this.dataListSelections = vals
        this.selectStbChange(this.data, val.siteId)
      },
      // 修改 终端名称
      addOrUpdateHandle: function (id) {
        if (this.selectedNode.data == null && id == null) {
          this.$message.error('未选择目录')
          return
        }
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, id != null ? 0 : this.selectedNode.data.id)
        })
      },
      subPlayHandle () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('至少选择一个终端')
          return
        }
        this.subPlayVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.subPlay.init(ids)
        })
      },
      addPlayHandle () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('至少选择一个终端')
          return
        }
        this.addPlayVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        console.log(ids)
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.addPlay.init(ids)
        })
      },
      cleanPrograme(id){
        if (this.dataListSelections.length === 0) {
          this.$message.error('至少选择一个终端')
          return
        }
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$http({
          url: this.$http.adornUrl('/generator/stb/clean'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })





      },
      // 远程安装，远程卸载
      remoteInstall (dataType) {
        if (this.dataListSelections.length === 0) {
          this.$message.error('请选择分组或者终端')
          return
        }
        this.stbInstallVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.mac
        })
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.stbInstall.init(ids, dataType)
        })
      },
      setVolHandle () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('至少选择一个终端')
          return
        }
        this.setVolVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        console.log(ids)
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.setVol.init(ids)
        })
      },
      msgHandle () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('至少选择一个终端')
          return
        }
        this.setVolVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.msg.init(ids)
        })
      },
      screenShotHandle () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('请选择一个终端')
          return
        }
        if (this.dataListSelections.length > 1) {
          this.$message.error('只能选择一个终端')
          return
        }
        if (this.dataListSelections[0].onlineSta) {
          this.$message.error('只能选择一个终端')
          return
        }
        this.$refs.screenShot.init(this.dataListSelections[0].mac)
      },
      // 远程安装，远程卸载
      switchSet () {
        if (this.dataListSelections.length === 0) {
          this.$message.error('请至少选择一个终端')
          return
        }
        this.stbSwtichVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$nextTick(function () {
          // 此时已经渲染完成
          this.$refs.stbSwtich.init(ids)
        })
      },
      // 关机，重启
      remoteBoot (dataType) {
        if (this.dataListSelections.length === 0) {
          this.$message.error('请至少选择一个终端')
          return
        }
        this.remoteInstallVisible = true
        var ids = this.dataListSelections.map(item => {
          return item.mac
        })
        var remoteInstall = {}
        remoteInstall.macs = ids
        remoteInstall.installType = dataType
        this.$http({
          url: this.$http.adornUrl('/generator/stb/remoteBoot'),
          method: 'post',
          data: this.$http.adornData(remoteInstall, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/stb/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      getGroups () {
        const self = this
        const url = '/generator/sgroup/goupList'
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            self.data = data.data
          } else {
            // alert(data.msg)
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      saveGroup (callback) {
        const node = this.curNode
        const url = node.id == null ? '/generator/sgroup/save' : '/generator/sgroup/update'
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'post',
          data: this.$http.adornData(node)
        }).then(({data}) => {
          if (data && data.code === 0) {
            callback(data)
          } else {
            // alert(data.msg)
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
      },
      addRoot () {
        this.curNode = {}
        this.curNode.parentId = 0
        this.showAddGroup = true
      },
      saveRoot () {
        const self = this
        this.saveGroup(function (r) {
          self.showAddGroup = false
          self.append(r.data)
        })
      },
      getRoot (node) {
        if (node.data.parentId === 0) {
          return node.data.id
        } else {
          return this.getRoot(this.$refs.tree.getNode(node.data.parentId))
        }
      },
      reload: function (event) {
        this.getDataList()
      },
      append (data) {
        data.children = []
        if (data.parentId === 0) {
          this.data.push(data)
        } else {
          this.$refs.tree.append(data, this.pNode)
        }
      },
      addNode (node) {
        this.pNode = node
        this.curNode = {}
        this.curNode.parentId = node.data.id
        this.showAddGroup = true
      },
      remove (node, data) {
        this.$refs.tree.remove(node)
      },
      delNode (node, data) {
        this.$confirm('此操作将永久删除该分组, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var url = '/generator/sgroup/delete/' + node.data.id
          this.$http({
            url: this.$http.adornUrl(url),
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.remove(node, data)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message({
                type: 'error',
                message: data.msg
              })
            }
          })
        }).catch(() => {
        })
      },
      edit (node) {
        this.curNode = node.data
        this.showEditGroup = true
      },
      saveEdit () {
        const self = this
        this.saveGroup(function (r) {
          self.showEditGroup = false
        })
      },
      hideEdit (node) {
        node.data.s_edit = false
        node.data.name = node.data.name.trim()
      },
      showEdit (node) {
        node.data.s_edit = true
        node.data.name = node.data.name + ' '
      },
      select (data, node, ele) {
        if (this.selectedNode.data) {
          // console.log(this.selectedNode)
          this.selectedNode.data.select = false
          this.selectedNode.data.name = ' ' + this.selectedNode.data.name
          this.selectedNode.data.name = this.selectedNode.data.name.trim()
        }
        data.select = true
        data.name = ' ' + node.data.name
        this.selectedNode = node
        this.getDataList()
      },
      view (id) {
        this.$router.push('/device-info?id=' + id)
      },
      checkChange (data, checked) {
        // if (this.$refs.tree.getHalfCheckedKeys().indexOf(data.id) > -1) {
        //   return
        // }
        console.log(data)
        if (checked.checkedKeys.indexOf(data.id) > -1) {
          this.checkStbByGroup(data.id, this.addStb)
        } else {
          this.checkStbByGroup(data.id, this.subStb)
        }
      },
      checkStbByGroup (gid, callback) {
        this.$http({
          url: this.$http.adornUrl('/generator/stb/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 1000,
            'stb_group_id': gid
          })
        }).then(({data}) => {
          if (data.code === 0) {
            callback(data.page.list)
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      addStb (data) {
        console.log('data', data)
        for (var i = 0; i < data.length; i++) {
          console.log('data[i]', data[i])
          console.log('dataListSelections', this.dataListSelections)
          if (this.indexOfSelections(data[i].id) < 0) {
            let index = this.indexOfData(data[i].id)
            if (index < 0) {
              this.$refs.multipleTable.toggleRowSelection(data[i], true)
              console.log('.....')
              this.dataListSelections.push(data[i])
            } else {
              console.log('.....')
              this.$refs.multipleTable.toggleRowSelection(this.dataList[index], true)
              this.dataListSelections.push(this.dataList[index])
            }
          }
        }
        console.log('dataListSelections', this.dataListSelections)
        // this.getDataList()
      },
      subStb (data) {
        console.log('data', data)
        var old = JSON.parse(JSON.stringify(this.dataListSelections))
        // for (var i = 0; i < data.length; i++) {
        //   console.log(data[i])

        //   var index = this.indexOfSelections(data[i].id)
        //   console.log(index)
        //   if (index > -1) {
        //     console.log('index', this.dataListSelections[index])
        //     this.$refs.multipleTable.toggleRowSelection(this.dataListSelections[index], false)
        //     // this.dataListSelections.splice(index, 1)
        //   }
        // }
        this.$refs.multipleTable.clearSelection()
        this.dataListSelections = old
        console.log('old', old)
        for (var i = 0; i < data.length; i++) {
          console.log(data[i])
          var index = this.indexOfSelections(data[i].id)
          console.log(index)
          if (index > -1) {
            this.dataListSelections.splice(index, 1)
          }
        }
        var newDate = JSON.parse(JSON.stringify(this.dataListSelections))
        this.dataListSelections = []
        this.addStb(newDate)
        // this.getDataList()
      },
      indexOfSelections (id) {
        for (var i = 0; i < this.dataListSelections.length; i++) {
          if (this.dataListSelections[i].id === id) {
            return i
          }
        }
        return -1
      },
      indexOfData (id) {
        for (var i = 0; i < this.dataList.length; i++) {
          if (this.dataList[i].id === id) {
            return i
          }
        }
        return -1
      },
      selectStbChangeAll (data) {
        for (var i = 0; i < data.length; i++) {
          var group = data[i]
          if (group.id != null) {
            if (this.getStbCheckCount(group) < group.stbCount) {
              this.$refs.tree.setChecked(group.id, false, true)
            } else {
              this.$refs.tree.setChecked(group.id, true, true)
            }
          }
          if (group.children != null) {
            this.selectStbChangeAll(group.children)
          }
        }
      },
      selectStbChange (data, sid) {
        for (var i = 0; i < data.length; i++) {
          var group = data[i]
          console.log('group', group)
          if (group.id != null && this.inSites(group.sites, sid)) {
            console.log(this.inSites(group.sites, sid))
            if (this.getStbCheckCount(group) < group.stbCount) {
              this.$refs.tree.setChecked(group.id, false, true)
            } else {
              this.$refs.tree.setChecked(group.id, true, true)
            }
          }
          if (group.children != null) {
            this.selectStbChange(group.children, sid)
          }
        }
      },
      getStbCheckCount (group) {
        var count = 0
        for (var i = 0; i < this.dataListSelections.length; i++) {
          if (this.inSites(group.sites, this.dataListSelections[i].siteId)) {
            count++
          }
        }
        console.log('count', count)
        return count
      },
      inSites (sites, sid) {
        for (var i = 0; i < sites.length; i++) {
          if (sites[i].id === sid) {
            return true
          }
        }
        return false
      },
      // 删除
      changeStatus (id) {
        this.$confirm(`确定要修改状态?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/stb/updateStatus'),
            method: 'post',
            data: id
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      leadHandle () {
        this.$message({
          message: '正在下载模板',
          type: 'success',
          duration: 1500
        })
        var self = this
        let token = this.$cookie.get('token')
        setTimeout(function () {
          window.location.href = self.$http.adornUrl('/generator/stb/dltmp' + '?token=' + token)
        }, 200)
      },
      sourceSuccess (response, file, fileList) {
        if (response.code === 0) {
          this.getDataList()
          this.$message({
            message: '导入成功',
            type: 'success',
            duration: 1500
          })
        } else {
          this.$message.error(response.msg)
        }
      }
    }
  }
</script>
